





use "raw data\raw_billing_data_summer_part_1", clear
keep if inrange(hour,14,17)
keep if inrange(time,33,46)
foreach num of numlist 2/6{
append using "raw data\raw_billing_data_summer_part_`num'"
keep if inrange(hour,14,17)
keep if inrange(time,33,46)
}


keep if inrange(hour,14,17)
drop naics //new 2/27
gen dow = dow(date) //new 2/27
keep if inrange(dow,1,5)
drop dow

//generated in step 1
merge m:1 sa using "stata data\TOU_demographic sm_date"
keep if _merge == 3
drop _merge

gen sm_date_ym = mofd(sm_date)
gen date_ym = mofd(date)
capture gen time_on_sm = 659 - sm_date_ym
//*/

tab time_on_sm

keep if inrange(time_on_sm,30,50)


gduplicates drop


keep if rs == "HA1X" //Make sure only have HA1X establishments, which are A1 establishments with smart meters

//drop any people who ever have return on their saids. These people have solar and I am not able to measure their impacts since I only have net consumption data on them. It's a small fraction of the sample
gen return = 0
replace return = 1 if dir == "R"
egen return_drop = max(return), by(sa)
drop if return_drop == 1
drop return_drop return dir


//now merge in pdp status
merge m:1 sa using "stata data\PDP extract 2015_0713 data - useful" //generated in step 1 from confidential data
drop if _merge == 2
capture drop _merge


replace pdp = 0 if pdp == .




merge m:1 date using "stata data/2015 pdp event dates" //merge in public information on 2015 event dates 
drop if _merge == 2

gen event_day = 0
replace event_day =1 if _merge == 3
drop _merge


//define eligiblity and pdp participation
gen eligible = 0
replace eligible = 1 if time >=40 & date > 20028 //set the date when people are eligible to be on 

rename pdp pdp_always_on
gen pdp = pdp_always_on
replace pdp = 0 if date <=20028



capture gen date_ym =mofd(date) 
gen ln_kwh = ln(kwh)

gen  days_before = -1*(sm_date - 18870)
gen  bin = round((days_before+4)/7)
replace bin = bin -1 if bin <=0

compress *

save "stata data\A1 PDP 2014 changeover for regs", replace


use "stata data\A1 PDP 2014 changeover for regs", clear


use sa time rs  bin using "stata data\A1 PDP 2014 changeover for regs", replace
gduplicates drop
save "stata data/Sa rs for 32 to 45 april 11 2016", replace //this is used much later 




